package com.demo.pattern.proxy.dynamicProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author linchong
 * @version 1.0
 * @Date: 2022-05-26 22:36
 * @Description: JDKProxy$
 */
public class JDKProxy {
	public static void main(String[] args) {
		//委托对象，委托方
		IRentingHouse rentingHouse = new RentingHouseImpl();

		/*//动态的获取代理对象，没有代理类
		IRentingHouse rentingHouseProxy = (IRentingHouse) Proxy.newProxyInstance(rentingHouse.getClass().getClassLoader(),
				rentingHouse.getClass().getInterfaces(),
				(proxy, method, args1) -> {
					Object result = null;
					//增强逻辑
					System.out.println("中介(代理)收取服务费3000元");
					//调用原有逻辑
					result = method.invoke(rentingHouse, args1);
					System.out.println("客户信息买了3元");
					return result;
				});*/
		//通过代理对象工厂获取代理对象
		IRentingHouse rentingHouseProxy = (IRentingHouse) ProxyFactory.getInstance().getJdkProxy(rentingHouse);
		rentingHouseProxy.rentHouse();
	}
}
